package com.csyf.manager.dao;

import com.csyf.manager.entity.SysTradeSummary;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @Author: Enzo
 * @Date: 2018/3/31 16:57
 */
public interface SysTradeSummaryDao extends Mapper<SysTradeSummary> {

    /**
     * @return
     */
    @Insert("INSERT  INTO `sys_order_summary` (agent_id,agent_name,trade_time,mch_id,service_type,total_count ,success_money,fail_money,success_count,fail_count,profit)\n" +
            " SELECT \n" +
            "  agent_id,agent_name,DATE_FORMAT(create_time,'%Y%m%d'),mch_id,service_type,\n" +
            "  COUNT(1),\n" +
            "  SUM(\n" +
            "    CASE\n" +
            "      WHEN trade_status = 'TRADE_SUCCESS' \n" +
            "      THEN total_fee \n" +
            "      ELSE 0\n" +
            "    END\n" +
            "  ) success_money,\n" +
            "  SUM(\n" +
            "    CASE\n" +
            "      WHEN trade_status <> 'TRADE_SUCCESS' \n" +
            "      THEN total_fee \n" +
            "      ELSE 0\n" +
            "    END\n" +
            "  ) fail_money,\n" +
            "  COUNT(\n" +
            "    CASE\n" +
            "      WHEN trade_status = 'TRADE_SUCCESS' \n" +
            "      THEN 1 \n" +
            "      ELSE NULL\n" +
            "    END\n" +
            "  ) success_count,\n" +
            "  COUNT(\n" +
            "    CASE\n" +
            "      WHEN trade_status <> 'TRADE_SUCCESS' \n" +
            "      THEN 1 \n" +
            "      ELSE NULL\n" +
            "    END\n" +
            "  ) fail_count,SUM(CASE WHEN trade_status = 'TRADE_SUCCESS' THEN profit ELSE 0 END) \n" +
            "FROM\n" +
            "  `sys_trade`  WHERE DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(#{day},'%Y-%m-%d') \n" +
            "GROUP BY agent_id ,agent_name,mch_id,service_type,DATE_FORMAT(create_time,'%Y%m%d')")
    Integer batchInsert(@Param("day") String day);


    @SelectProvider(type = CurDataSqlProvider.class, method = "selectCurdata")
    List<SysTradeSummary> queryTrade(SysTradeSummary vo);


    @Insert("INSERT INTO `sys_order_profit` (src_mch_id,mch_id,service_type,trade_count,trade_totalmoney,cost_profit,mch_profit,ctime)\n" +
            "SELECT src_mch_id,mch_id,service_type,COUNT(1),SUM(total_fee),SUM(costprofit),SUM(profit),DATE_FORMAT(pay_time,'%Y-%m-%d') \n" +
            "FROM `sys_trade`  \n" +
            "WHERE DATE_FORMAT(pay_time,'%y-%m-%d') =  DATE_FORMAT(#{time},'%y-%m-%d') \n" +
            "AND trade_status = 'TRADE_SUCCESS'\n" +
            " GROUP BY src_mch_id ,mch_id,service_type,DATE_FORMAT(pay_time,'%Y-%m-%d') ")
    Integer batchPoriftInsert(@Param("time") String time);
}
